<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\Db;

use app\common\model\Category as CategoryModel;

/**
 * @title 栏目管理
 * @description 接口说明
// * @header name:key require:1 default: desc:秘钥(区别设置)
// * @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class Category extends Base
{
    /**
     * @title 获取栏目
     * @description 接口说明：获取网站顶部栏目
     * @author 开发者
     * @url /api/Category/list
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     * @param name:parentid type:int require:1 default:0 other: desc:0=第一层
     * @param name:isPage type:bool require:1 default:0 other: desc:1=分页，2=全遍历
     *
//     * @return name:名称
//     * @return mobile:手机号
//     * @return list_messages:消息列表@
//     * @list_messages message_id:消息ID content:消息内容
//     * @return object:对象信息@!
//     * @object attribute1:对象属性1 attribute2:对象属性2
//     * @return array:数组值#
//     * @return list_user:用户列表@
//     * @list_user name:名称 mobile:手机号 list_follow:关注列表@
//     * @list_follow user_id:用户id name:名称
     */
    public function list()
    {
        $param = input("param.");
        $siteid = isset($param['siteid'])?$param['siteid']:1;

        $CategoryModel = new CategoryModel();
        if(!isset($param['parentid'])){$param['parentid'] = 0;}
        $field='*';
        if($param['parentid'] == ''){
            $field='id,catname,catname1,parentid,module,arrparentid,orderid,picurl,url,template_list,template_show,add_time';
        }
        $param['is_open'] = 1;
        $param['$siteid'] = $siteid;
        $isPage = isset($param['isPage']) && $param['isPage'] == 2 ?false:true;
        $res = $CategoryModel -> tableData($param,$field,$isPage);
        return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
    }

    /**
     * @title 获取所有栏目（无限级）
     * @description 接口说明：获取无限级栏目,siteid ： 站点id，默认1
     * @author 开发者
     * @url /api/Category/listMoreChild
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     *
     */
    public function listMoreChild(){
        $param = input("param.");
        $siteid = isset($param['siteid'])?$param['siteid']:1;
        $CategoryModel = new CategoryModel();
        $field='id,catname,catname1,parentid,module,arrparentid,orderid,picurl,url,template_list,template_show,add_time';
        $list = $CategoryModel
            ->where('siteid',"=",$siteid)
            ->where([
                'parentid'=>0,
                'is_open'=>1
            ])
            ->field($field)
            ->order('orderid asc,id desc')->select()->toArray();

        foreach ($list as $k=>$v){
            $list[$k]['level'] = 1;
            $child_list = $CategoryModel->getChildList($v['id'],2);

            foreach ($child_list as $k2=>$v2){
                if(count($v2['child']) == 0 ){
                    //获取当前栏目的产品'orderid' => 'asc', 'id' => 'desc'
                    $child = Db::name("product")->where("catids","find in set",$v2['id'])->where("status","=",1)->order("orderid asc,id desc")->select();
                    $child_list[$k2]['child_pro'] = $child;
                }
            }

            $list[$k]['child_list'] = $child_list;
        }
        return dyajaxReturn(1,'获取成功',$list);
    }

    /**
     * @title 获取指定栏目所有下级（无限级）
     * @description 接口说明：获取网站顶部栏目
     * @author 开发者
     * @url /api/Category/getChildList
     * @method GET POST
     *
     * @param name:cate_id type:int require:1 default:0 other: desc:栏目id
     */
    public function getChildList(){
        $param = input('param.');
        if(!isset($param['cate_id'])){
            return dyajaxReturn(0,'缺少关键参数cate_id');
        }

        $CategoryModel = new CategoryModel();
        $list = $CategoryModel->getChildList($param['cate_id']);

        return dyajaxReturn(1,'获取成功',$list);
    }

    /**
     * @title 获取单栏目详情
     * @description 接口说明：获取单栏目详情
     * @author 开发者
     * @url /api/Category/getInfo
     * @method GET POST
     *
     * @param name:cate_id type:int require:1 default:0 other: desc:栏目id
     */
    public function getInfo(){
        $param = input("param.");
        $id = $param['cate_id'];

        $CategoryModel = new CategoryModel();
        $info = $CategoryModel -> where("id",'=',$id)->find();

        return dyajaxReturn(1,'获取成功',$info);
    }

    /**
     * @title 获取头部SEO信息
     * @description 接口说明：获取头部信息
     * @author 开发者
     * @url /api/Category/GetApiHeader
     * @method GET POST
     *
     * @param name:siteid type:int require:0 default:1 other: desc:站点id，默认1
     * @param name:catid type:int require:1 default:0 other: desc:栏目id
     * @param name:id type:int require:1 default:0 other: desc:详情页信息，可不传
     */
    public function GetApiHeader(){
        $param = input("param.");
        $siteid = isset($param['siteid'])?$param['siteid']:1;

        if(!isset($param['catid']) && $param['catid'] == ''){
            return dyajaxReturn(0,'请输入栏目id');
        }
        $catid = $param['catid'];
        $id = $param['id'];
        $res = GetApiHeader($siteid,$catid,$id);
        return dyajaxReturn(1,'获取成功',$res);
    }

}
